import pymysql
from config import Config
import hashlib

def verify_user(username, password):
    """验证用户名和密码"""
    try:
        #假设验证正确。
        return True
        """
        
        # 使用配置文件中的数据库连接信息
        conn = pymysql.connect(** Config.SQL_CON_STR)
        cursor = conn.cursor()
        
        # 查询用户密码
        sql = "SELECT password FROM users WHERE username = %s"
        cursor.execute(sql, (username,))
        result = cursor.fetchone()
        
        cursor.close()
        conn.close()
        
        # 使用密码哈希验证
        if result:
            stored_password = result[0]
            # 这里简单对比，实际应该是使用哈希算法比较
            return stored_password == password
            
        return False
        """
        
    except Exception as e:
        raise Exception(f'数据库错误: {str(e)}')


def get_store_performance(start_date, end_date):
    """查询指定时间段内各店铺的业绩总和"""
    try:
        # 连接数据库
        conn = pymysql.connect(**Config.SQL_CON_STR)
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        
        # 查询各店铺业绩总和（请根据实际表结构修改SQL）
        sql = """
        SELECT 
            shop_name, 
            SUM(amount) as total_amount 
        FROM 
            sales_records 
        WHERE 
            sale_time BETWEEN %s AND %s
            AND shop_name IN ('A1', 'A2', 'A3')
        GROUP BY 
            shop_name
        ORDER BY 
            shop_name
        """
        
        cursor.execute(sql, (start_date, end_date))
        results = cursor.fetchall()
        
        cursor.close()
        conn.close()
        
        return results
        
    except Exception as e:
        raise Exception(f'查询业绩时发生错误: {str(e)}')
